package com.service.impl;

import com.bean.BookCategory;
import com.mapper.BookCategoryMapper;
import com.service.BookCategoryService;
import com.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BookCategoryServiceImpl implements BookCategoryService {

    @Autowired
    private BookCategoryMapper bookCategoryMapper;

    @Override
    public BookCategory getBookCategoryById(Integer categoryId) {
        return bookCategoryMapper.getBookCategoryById(categoryId);
    }

    @Override
    public List<BookCategory> getAllBookCategories() {
        return bookCategoryMapper.getAllBookCategories();
    }

    @Override
    public boolean insertBookCategory(BookCategory bookCategory) {
        int result = bookCategoryMapper.insertBookCategory(bookCategory);
        return result > 0;
    }

    @Override
    public boolean updateBookCategory(BookCategory bookCategory) {
        int result = bookCategoryMapper.updateBookCategory(bookCategory);
        return result > 0;
    }

    @Override
    public boolean deleteBookCategoryById(Integer categoryId) {
        int result = bookCategoryMapper.deleteBookCategoryById(categoryId);
        return result > 0;
    }

    @Override
    public PageInfo<BookCategory> getBookCategoriesByKeyword(String keyword, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<BookCategory> categories = bookCategoryMapper.getBookCategoriesByKeyword(keyword, offset, pageSize);
        int totalCount = bookCategoryMapper.countBookCategoriesByKeyword(keyword);
        return new PageInfo<>(pageNum, pageSize, totalCount, categories);
    }
}